<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <Authority :value="'role.add'">
          <a-button type="primary" @click="handleCreate"> 新增角色 </a-button>
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              label: '菜单',
              auth: 'role.view',
              onClick: handleSetView.bind(null, record),
            },
            {
              label: '接口',
              auth: 'role.api',
              onClick: handleSetApi.bind(null, record),
            },
            {
              icon: 'clarity:note-edit-line',
              auth: 'role.edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              auth: 'role.del',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <SetApiDrawer @register="registerSetApiDrawer" @success="handleSuccess" />
    <SetViewDrawer @register="registerSetViewDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { getRolePage, removeRole } from '/@/api/system/role';
import { useDrawer } from '/@/components/Drawer';
import { Authority } from '/@/components/Authority';
import RoleDrawer from './RoleDrawer.vue';
import { columns, searchFormSchema } from './role.data';
import SetApiDrawer from './SetApiDrawer.vue';
import SetViewDrawer from './SetViewDrawer.vue';

const [registerDrawer, { openDrawer }] = useDrawer();
const [registerSetApiDrawer, setApiMethods] = useDrawer();
const [registerSetViewDrawer, setViewMethods] = useDrawer();
const [registerTable, { reload }] = useTable({
  title: '角色列表',
  api: getRolePage,
  columns,
  formConfig: {
    labelWidth: 90,
    schemas: searchFormSchema,
  },
  useSearchForm: true,
  showTableSetting: true,
  bordered: true,
  showIndexColumn: false,
  actionColumn: {
    width: 180,
    title: '操作',
    dataIndex: 'action'
  },
});

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

async function handleDelete(record: Recordable) {
  await removeRole([record['id']]);
  reload();
}

function handleSuccess() {
  reload();
}

function handleSetApi(record: Recordable) {
  setApiMethods.openDrawer(true, {
    record,
    isUpdate: true,
  })
}
function handleSetView(record: Recordable) {
  setViewMethods.openDrawer(true, {
    record,
    isUpdate: true,
  })
}
</script>